package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.cache.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.repository.UserInfoRepository;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;
import java.util.regex.Pattern;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository repository;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = repository.findByPhone(phone);
        return userInfo == null;
    }

    @Override
    public void sendVerifyCode(String phone, String smsUrl, String smsMsg, String smsAppkey) {

        // 生成验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);

        // 发送短信
        StringBuilder stringBuilder = new StringBuilder(100);
        stringBuilder.append("您的验证码为").append(code).append(",请输入");

        System.out.println(stringBuilder.toString()); //假装已经发送了
        //真实发送短信:原理:使用java方式发起http请求调用网关暴露接口
        //https ://way. jd. com/chonry/smsnotice?sign={1}&mobile={2}&content={3}&appkey={4}
        //java中发送http请求
        RestTemplate template = new RestTemplate();
        String ret = template.getForObject(smsUrl, String.class, phone, smsMsg, smsAppkey);
        System. out. println(ret);
        if(!ret.contains("查询成功")) {
            throw new LogicException("短信发送失败");
        }
        //缓存验证码: redis有效时间5分钟
        userInfoRedisService.setVerifyCode(phone,code);
    }

    @Override
    public void regist(String verifyCode, String phone, String nickname, String password, String rpassword) {
         /* String code = userInfoRedisService.getVerifyCode(phone);
         return verifyCode == code;*/

         //1:判断参数是否为null
         AssertUtil.hasLength(phone,"手机号码不能为空");
         AssertUtil.hasLength(nickname,"昵称不能为空");
         AssertUtil.hasLength(password,"密码不能为空");
         AssertUtil.hasLength(rpassword,"确认密码不能为空");
         AssertUtil.hasLength(verifyCode,"验证码不能为空");
         //2:判断2次密码是否一致
         AssertUtil.isEquals(password,rpassword,"2次输入密码不一致");
         //3:判断手机号码格式是否正确 TODO JAVA正则
         System.out.println(phone);
         String pattern = "^1[3456789]\\d{9}$";
         boolean matches = Pattern.matches(pattern, phone);
         if(!matches){
             throw new LogicException("手机号格式有误");
         }
         //4:判断手机号码是否唯一
         if (!this.checkPhone(phone)){
             throw new LogicException("手机号已经被注册");
         }
         //5:判断验证码是否一致
         //redis 的 code
         String code = userInfoRedisService.getVerifyCode(phone);
         if(code == null || !code.equalsIgnoreCase(verifyCode)){
              throw new LogicException("验证码失效或者错误");
         }
         //6:注册
         UserInfo userInfo = new UserInfo();
         userInfo.setNickname(nickname);
         userInfo.setPhone(phone);
         userInfo.setPassword(password);// 假装加密了
         userInfo.setEmail("");
         userInfo.setCity("");
         userInfo.setInfo("");
         userInfo.setLevel(1);

         userInfo.setHeadImgUrl("/images/default.jpg");
         userInfo.setState(UserInfo.STATE_NORMAL);

         this.save(userInfo);
    }

    @Override
    public void save(UserInfo userInfo) {
        /*userInfo.setEmail("");
        userInfo.setCity("");
        userInfo.setInfo("");
        userInfo.setLevel(1);
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setState(UserInfo.STATE_NORMAL);*/
        repository.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {

        // 查询用户对象
        //UserInfo userInfo = repository.findByPhoneAndPassword(username,password);
        UserInfo userInfo = repository.findByPhone(username);

        if (userInfo == null || !password.equals(userInfo.getPassword())) {
            throw new LogicException("账号或密码出错");
        }

        userInfo.setPassword(null); // 不要将密码传入到页面
        return userInfo;
    }
}
